package com.portal.gui.marketman;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Customer;
import com.portal.logic.CustomerDAO;
import com.portal.logic.Product;

public class SelectCustomerToEditScr extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectCustomerToEditScr frame = new SelectCustomerToEditScr();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SelectCustomerToEditScr() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(12, 167, 97, 25);
		contentPane.add(btnBack);
		
		Connection connection = ConnectionManager.getConnection();
		final CustomerDAO customerDAO = new CustomerDAO(connection) ;
		List<Customer> customers = customerDAO.selectAll();

		final JComboBox cmbCustomers = new JComboBox();
		Iterator<Customer> iterator = customers.iterator();
		while (iterator.hasNext()) {
			Customer customer = (Customer) iterator.next();
			cmbCustomers.addItem(customer.getName());
		}
		cmbCustomers.setBounds(179, 42, 116, 22);
		contentPane.add(cmbCustomers);
		
		JLabel lblSelectCustomer = new JLabel("Select Customer:");
		lblSelectCustomer.setBounds(51, 45, 116, 16);
		contentPane.add(lblSelectCustomer);
		
		JButton btnStartEditCustomer = new JButton("Edit Customer");
		btnStartEditCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				String selectedProduct = (String)cmbCustomers.getSelectedItem();
				EditCustomerScr editProductScr = new EditCustomerScr(selectedProduct);
				editProductScr.setVisible(true);
			}
		});
		btnStartEditCustomer.setBounds(173, 108, 146, 25);
		contentPane.add(btnStartEditCustomer);
	}

}
